package by.averianov.discounta.webapp.page.table;

import java.io.Serializable;
import java.util.HashMap;

import javax.inject.Inject;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.Page;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.hibernate.mapping.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import by.averianov.discounta.datamodel.AbstractEntity;
import by.averianov.discounta.datamodel.Card;
import by.averianov.discounta.datamodel.CardType;
import by.averianov.discounta.datamodel.PriceBook;
import by.averianov.discounta.datamodel.Product;
import by.averianov.discounta.datamodel.Shop;
import by.averianov.discounta.datamodel.UserProfile;
import by.averianov.discounta.services.UserService;
import by.averianov.discounta.webapp.page.administrator.Administrator;
import by.averianov.discounta.webapp.page.pricebook.Economist;
import by.averianov.discounta.webapp.page.shop.InShop;

public class Row extends Panel{

	private static final Logger LOGGER = LoggerFactory.getLogger(Row.class);

	private AbstractEntity entity;
	private int currentTab;
	private Long productShop;
	private Boolean isRowSelected;
	private String NameColumn2;
	private String NameColumn3;
	private String NameColumn4;
	private String NameColumn5;
	private String NameColumn6;
	private String NameColumn7;
	private String NameColumn8;
	private String currentMenu;
	
	private Label Colunm1;
	private Label Colunm2;
	private Label Colunm3;
	private Label Colunm4;
	private Label Colunm5;
	private Label Colunm6;
	private Label Colunm7;
	private Label Colunm8;

	public Row(String id, AbstractEntity entity, Boolean isRowSelected, String currentMenu) {
		
		super(id);
		this.entity = entity;
		this.isRowSelected = isRowSelected;
		this.currentMenu = currentMenu;

		this.NameColumn2 = "";
		this.NameColumn3 = "";
		this.NameColumn4 = "";
		this.NameColumn5 = "";
		this.NameColumn6 = "";
		this.NameColumn7 = "";
		this.NameColumn8 = "";
	}
	
	@Inject
	private UserService userService;
	
	@Override
	protected void onInitialize() {
		super.onInitialize();
		
		if(entity.getClass().equals(Shop.class)) {
			Shop shop = (Shop)entity;			
			this.currentTab = 0;
			
			NameColumn2 = shop.getNameShop();
		}
		
		if(entity.getClass().equals(Card.class)) {
			Card card = (Card)entity;
			this.currentTab = 1;
			
			NameColumn2 = card.getCardType().getNameCardType();
			NameColumn3 = card.getCardStatus().name();
			if(card.getUserProfile() != null) {
				NameColumn4 = userService.getUserAccount(card.getUserProfile().getId()).getUserEmail();
			}
			if(currentMenu == "profile") {
				NameColumn4 = "";
				NameColumn5 = "Процент по карте";
				NameColumn6 = "Сумма по карте";				
			} else {
				NameColumn5 = userService.getUserAccount(card.getUserEditor().getId()).getUserEmail();
				NameColumn6 = card.getCardDateCreate().toString();				
			}
			if(card.getCardDateStart() == null) {
				NameColumn7 = "";
			} else {
				NameColumn7 = card.getCardDateStart().toString();
			}
			
		}
		
		if(entity.getClass().equals(CardType.class)) {
			CardType cardType = (CardType)entity;			
			this.currentTab = 2;
			
			NameColumn2 = cardType.getNameCardType();			
		}
		
		if(entity.getClass().equals(PriceBook.class)) {
			PriceBook priceBook = (PriceBook)entity;			
			this.currentTab = 2;
			
			NameColumn2 = priceBook.getValueSumLimit().toString();
			NameColumn3 = priceBook.getValuePercentDiscount().toString();
			NameColumn4 = userService.getUserAccount(priceBook.getUserEditor().getId()).getUserEmail();			
		}
		
		if(entity.getClass().equals(UserProfile.class)) {
			UserProfile userProfile = (UserProfile)entity;
			this.currentTab = 3;
			
			NameColumn2 = userProfile.getUserFirstName();
			NameColumn3 = userProfile.getUserLastName();
			NameColumn4 = userProfile.getUserDateCreated().toString();
			NameColumn5 = userProfile.getPhoneNumber().toString();
			NameColumn6 = userProfile.getAccessGroup().name();
		}
		
		if(entity.getClass().equals(Product.class)) {
			Product product = (Product)entity;
			//this.currentTab = 3;
			this.productShop = product.getShop().getId();
			
			NameColumn2 = product.getNameProduct();
			NameColumn3 = product.getPriceProduct().toString();
			NameColumn4 = product.getDiscountStatus().toString();
			NameColumn5 = product.getShop().getNameShop();
			NameColumn6 = product.getCheckoutCounter().toString();
			NameColumn7 = product.getCountProduct().toString();
		}
		
		Colunm1 = new Label("Colunm1", new Model<Long>(entity.getId()));		
		Colunm2 = new Label("Colunm2", new Model<String>(NameColumn2));
		Colunm3 = new Label("Colunm3", new Model<String>(NameColumn3));
		Colunm4 = new Label("Colunm4", new Model<String>(NameColumn4));
		Colunm5 = new Label("Colunm5", new Model<String>(NameColumn5));
		Colunm6 = new Label("Colunm6", new Model<String>(NameColumn6));
		Colunm7 = new Label("Colunm7", new Model<String>(NameColumn7));
		Colunm8 = new Label("Colunm8", new Model<String>(NameColumn8));			
		
		Link linkRow = new Link("tableRow") {
			@Override
			public void onClick() {
				Page page = null;
				if(currentMenu == "administrator") {
					page = new Administrator(currentTab, entity.getId());
					LOGGER.debug("Select Row - " + entity.getId());
				};
				if(currentMenu == "priceBook") {
					page = new Economist(currentTab, entity.getId());
					LOGGER.debug("Select Row - " + entity.getId());
				};
				if(currentMenu == "shop") {
					Long sId = entity.getId();
					Long pShop = productShop;
					page = new InShop(sId, pShop);
					LOGGER.debug("Select Row - " + entity.getId());
				};
				setResponsePage(page);				
			}
		};		
		
		if(isRowSelected){
			linkRow.add(AttributeModifier.append("class", "table_row_selected"));
		}
		if(currentMenu == "profile") {
			LOGGER.debug("From UserProfilePage");
			linkRow.setEnabled(false);
		}
		linkRow.add(Colunm1);
		linkRow.add(Colunm2);
		linkRow.add(Colunm3);
		linkRow.add(Colunm4);
		linkRow.add(Colunm5);
		linkRow.add(Colunm6);
		linkRow.add(Colunm7);
		linkRow.add(Colunm8);
		add(linkRow);
	}

}